package com.drugStorage.struts.actions;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.drugStorage.domain.BuyList;
import com.drugStorage.domain.BuyPlan;
import com.drugStorage.domain.DrugScience;
import com.drugStorage.domain.DrugSupplier;
import com.drugStorage.domain.InDrugList;
import com.drugStorage.domain.InStorageBill;
import com.drugStorage.domain.User;
import com.drugStorage.spring.service.BuyPlanService;
import com.drugStorage.spring.service.DrugService;
import com.drugStorage.spring.service.DrugSupplierService;
import com.drugStorage.spring.service.InStorageService;
import com.drugStorage.struts.actionForms.BuyPlanActionForm;
import com.drugStorage.struts.actionForms.InStorageBillActionForm;

public class InStorageBillAction extends DispatchAction {

	private InStorageService inStorageService;

	private DrugService drugService;

	private DrugSupplierService drugSupplierService;

	public InStorageService getInStorageService() {
		return inStorageService;
	}

	public void setInStorageService(InStorageService inStorageService) {
		this.inStorageService = inStorageService;
	}

	public DrugService getDrugService() {
		return drugService;
	}

	public void setDrugService(DrugService drugService) {
		this.drugService = drugService;
	}

	public DrugSupplierService getDrugSupplierService() {
		return drugSupplierService;
	}

	public void setDrugSupplierService(DrugSupplierService drugSupplierService) {
		this.drugSupplierService = drugSupplierService;
	}

	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		inStorageService = (InStorageService) factory
				.getBean("inStorageService");

		List<InStorageBill> inStorageBillList = inStorageService
				.getInStorageBillList();
		System.out.println(inStorageBillList);
		request.setAttribute("inStorageBillList", inStorageBillList);
		return mapping.findForward("Show_InstorageBill");
	}

	public ActionForward getDrugLists(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");

		inStorageService = (InStorageService) factory
				.getBean("inStorageService");
		String bid = request.getParameter("id");
		int id = Integer.parseInt(bid);
		System.out.println(bid);
		List<InDrugList> inDrugList = inStorageService
				.getInDrugListByInstorageBillId(id);
		System.out.println(inDrugList);
		request.setAttribute("inDrugList", inDrugList);
		// request.getSession().setAttribute("buyLists", buyLists);
		return mapping.findForward("inDrugList_success");

	}

	/**
	 * 跳转到添加采购列表页面
	 */
	public ActionForward GoAddInDrugList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		drugService = (DrugService) factory.getBean("drugService");
		inStorageService = (InStorageService) factory
				.getBean("inStorageService");
		// drugSupplierService = (DrugSupplierService)
		// factory.getBean("drugSupplierService");
		List<DrugScience> DrugList = drugService.showDrugs();
		// List<DrugSupplier> drugSupplierList =
		// drugSupplierService.getDrugSupplierList();

		String bid = request.getParameter("id");

		int id = Integer.parseInt(bid);
		InStorageBill inStorageBill = inStorageService.getInStorageBill(id);
		request.setAttribute("inStorageBill", inStorageBill);
		request.setAttribute("DrugList", DrugList);
		System.out.println("xxxx");
		// request.setAttribute("drugSupplierList", drugSupplierList);
		return mapping.findForward("AddInDrugList");
	}

	/**
	 * 添加药品列表
	 * 
	 * 
	 */
	public ActionForward AddInDrugList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");

		inStorageService = (InStorageService) factory
				.getBean("inStorageService");

		drugService = (DrugService) factory.getBean("drugService");
		List<DrugScience> DrugList = drugService.showDrugs();
		String bid = request.getParameter("id");
		int id = Integer.parseInt(bid);
		System.out.println(bid);
		InStorageBillActionForm inStorageBillActionForm = (InStorageBillActionForm) form;
		int drugid = inStorageBillActionForm.getDrugId();
		int amount = inStorageBillActionForm.getAmount();
		float inUnitPrice = inStorageBillActionForm.getInUnitPrice();
		float outUnitPrice = inStorageBillActionForm.getOutUnitPrice();
		String bidType = inStorageBillActionForm.getBidType();
		String proDateStr = inStorageBillActionForm.getProDate();
		String deadDateStr = inStorageBillActionForm.getDeadDate();
		String lotNumber = inStorageBillActionForm.getLotNumber();
		String inVoiceNo = inStorageBillActionForm.getInVoiceNo();
		String note = inStorageBillActionForm.getNote();

		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

		Date Prodate = dateFormat.parse(proDateStr);
		Date deadDate = dateFormat.parse(deadDateStr);

		DrugScience drug = drugService.getDrugById(drugid);
		System.out.println(drugid);
		float inprice = inUnitPrice * amount;
		float outprice = outUnitPrice * amount;
		float margin = outprice - inprice;
		InDrugList inDrugList = new InDrugList();
		inDrugList.setAmount(amount);
		inDrugList.setBidType(bidType);
		inDrugList.setDeadDate(deadDate);
		inDrugList.setProDate(Prodate);
		inDrugList.setDrug(drug);
		inDrugList.setInPrice(inprice);
		inDrugList.setOutPrice(inprice*1.15F);
		inDrugList.setInUnitPrice(inUnitPrice);
		inDrugList.setInVoiceNo(inVoiceNo);
		inDrugList.setLotNumber(lotNumber);
		inDrugList.setMargin(margin);
		inDrugList.setNote(note);
		inDrugList.setOutUnitPrice(inUnitPrice*1.15F);

		inStorageService.AddInDrugListToInStorageBill(inDrugList, id);
		// List<BuyList> buyLists = buyPlanService.getAllListByPlanId(id);

		// request.setAttribute("buyLists", buyLists);
		// 药品的列表

		// request.setAttribute("DrugList", DrugList);

		// request.getSession().setAttribute("buyLists", buyLists);
		return mapping.findForward("AddInDrugList_success");

	}

	public ActionForward GoAddInStorageBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		drugSupplierService = (DrugSupplierService) factory
				.getBean("drugSupplierService");
		List<DrugSupplier> drugSupplierList = drugSupplierService
				.getDrugSupplierList();
		System.out.println(drugSupplierList);
		request.setAttribute("drugSupplierList", drugSupplierList);

		return mapping.findForward("AddInStorageBill");
	}

	public ActionForward AddInStorageBill(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		BeanFactory factory = new ClassPathXmlApplicationContext(
				"applicationContext-*.xml");
		inStorageService = (InStorageService) factory
				.getBean("inStorageService");

		User user = (User) request.getSession().getAttribute("user");
		System.out.println(user);
		InStorageBillActionForm inStorageBillActionForm = (InStorageBillActionForm) form;

		int sid = inStorageBillActionForm.getDrugSupplierId();
		
		String state = inStorageBillActionForm.getState();

		// String dateStr = inStorageBillActionForm.getDate();
		DrugSupplier drugSupplier = drugSupplierService.getDrugSupplier(sid);
		InStorageBill inStorageBill = new InStorageBill();
		inStorageBill.setOperator(user);

		// DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

		// Date date = dateFormat.parse(dateStr);
		// buyPlan.setDate(date);
        Date date = new Date();
		inStorageBill.setSupplier(drugSupplier);
		inStorageBill.setDate(date);
		inStorageBill.setState(state);
		inStorageService.saveInStorageBill(inStorageBill);
		return mapping.findForward("AddInDrugList_success");
	}

}
